package zw_901_1000.zw_942_增减字符串匹配;

public class Solution {
    public static void main(String[] args) {
        String s = "IDID";
        int[] match = diStringMatch(s);
        for (int i : match) {
            System.out.print(i + " ");
        }
    }

    public static int[] diStringMatch(String s) {
        int n = s.length(), l = 0, r = n, idx = 0;
        int[] ans = new int[n + 1];
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == 'I') {
                ans[idx++] = l++;
            } else {
                ans[idx++] = r--;
            }
//            ans[idx++] = s.charAt(i) == 'I' ? l++ : r--;
        }
        ans[idx] = l; // 最后剩下一个数，此时 l == r
        return ans;
    }
}
